﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Input;
using EnergyConsumptionMonitorSystem.Components;

namespace EnergyConsumptionMonitorSystem.MainWindowTabItem
{
    public abstract class TabItemContentUCViewModel : INotifyPropertyChanged
    {
        protected UserControl _view;
        public UserControl View { get { return this._view; } }

        protected MainWindowTabItemViewModel _containerVM;
        public MainWindowTabItemViewModel ContainerVM
        {
            get
            {
                return this._containerVM;
            }
            set
            {
                if (this._containerVM != value)
                {
                    if (this._containerVM != null)
                    {
                        this._containerVM.ClosingEvent -= this.tabItem_ClosingEvent;
                    }
                    this._containerVM = value;
                    if (value != null)
                    {
                        value.ClosingEvent += new System.ComponentModel.CancelEventHandler(tabItem_ClosingEvent);
                    }
                }
            }
        }

        private ICommand _closeRequestCommand;
        public ICommand CloseRequestCommand { get { return this._closeRequestCommand; } }

        protected abstract void tabItem_ClosingEvent(object sender, System.ComponentModel.CancelEventArgs e);

        public event EventHandler CloseRequestEvent;

        public TabItemContentUCViewModel()
        {
            this._closeRequestCommand = new Command(new Action<object>(this.CloseRequest));
        }

        public virtual void CloseRequest(object o)
        {
            if (this.CloseRequestEvent != null)
            {
                this.CloseRequestEvent(this, new EventArgs());
            }
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }
}
